package com.mashuai.utils.toolkit.core.msFile;

import com.mashuai.utils.base.MsIocUtils;
import com.mashuai.utils.toolkit.achieve.FileUtilsCore;
import com.mashuai.utils.toolkit.achieve.MsExegesis;
import com.mashuai.utils.toolkit.core.msFile.dto.MsAnalysisZipFileDataDto;
import com.mashuai.utils.toolkit.core.msFile.dto.MsDownloadFileDto;
import com.mashuai.utils.toolkit.core.msFile.dto.MsDownloadFileZipDto;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

/**
 * @ClassName: MsFileUtils
 * @Description: 文件相关
 */
@MsExegesis("")
public class MsFileUtils {
    private static FileUtilsCore fileUtilsCore = MsIocUtils.getBean(FileUtilsCore.class);

    /**
     * 根据url下载文件
     *
     * @param response
     * @param dto
     * @return
     * @author mashuai
     */
    @MsExegesis("根据url下载文件")
    public static void downloadFile(HttpServletResponse response, MsDownloadFileDto dto)  {
        try {
            fileUtilsCore.downloadFile(response, dto);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /***
     * 把文件下载成zip
     * @param response
     * @param dto
     * @return
     * @author mashuai
     */
    @MsExegesis("把文件下载成zip")
    public static void downloadFileZip(HttpServletResponse response, MsDownloadFileZipDto dto){
        try {
            fileUtilsCore.downloadFileZip(response, dto);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 从下载链接中提取，文件的zip中的json数据
     *
     * @param dto
     * @return
     * @author mashuai
     */
    @MsExegesis("从下载链接中提取，文件的zip中的json数据")
    public static Map<String, Object> analysisZipFileData(MsAnalysisZipFileDataDto dto) {
        try {
            return fileUtilsCore.analysisZipFileData(dto);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 读取resources目录下文件输入流
     *
     * @param relativePath 相对项目下resources目录的路径，比如 /static/1.txt
     * @return
     * @author mashuai
     */
    @MsExegesis("读取resources目录下文件输入流。relativePath@相对项目下resources目录的路径，比如 /static/1.txt")
    public static InputStream getResourcesFile(String relativePath) {
        try {
            return FileUtilsCore.getResourcesFile(relativePath);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * InputStream inputStream 转 File file对象。会在内存中创建一个临时变量
     *
     * @param inputStream
     * @param fileSuffix  文件后缀
     * @return
     * @author mashuai
     */
    @MsExegesis("InputStream inputStream 转 File file对象。会在内存中创建一个临时变量")
    public static File inputStreamConversionFile(InputStream inputStream, String fileSuffix) {
        return FileUtilsCore.inputStreamConversionFile(inputStream, fileSuffix);
    }

    /**
     * 读取文件流中的数据，返回String
     *
     * @param inputStream
     * @return
     * @author mashuai
     */
    @MsExegesis(" 读取文件流中的数据，返回String")
    public static String readInputStreamAsString(InputStream inputStream) {
        try {
            return FileUtilsCore.readInputStreamAsString(inputStream);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
